package com.miracle9.game.dao;

import com.miracle9.game.entity.User;
import com.miracle9.game.entity.UserTop;
import com.miracle9.game.util.MyUtil;
import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Repository
public class UserTopServiceDao extends SqlOperating<UserTop, Integer> {

    private Logger logger = Logger.getLogger(UserTopServiceDao.class);

    @Cacheable(value = {"springCache"}, key = "'topList'+#type+#topType")
    public List<UserTop> topList(int type, int topType) {
        //日榜
        if(topType == 1){
            String datetime = MyUtil.dateFormatYMD_Sting(new Date());
            Query query = q_CreateSql("from UserTop where type=? and datetime >=? order by gold desc", new Object[]{type, datetime});
            query.setMaxResults(10);
            return query.list();
        }
        //周榜
        if (topType == 2) {
            Calendar c = Calendar.getInstance();
            c.set(Calendar.DAY_OF_WEEK, 2);
            String datetime = MyUtil.dateFormatYMD_Sting(c.getTime());
            Query query = q_CreateSql("from UserTop where type=? and datetime >=? order by gold desc", new Object[]{type, datetime});
            query.setMaxResults(10);
            return query.list();
        }
        //总榜
        if (topType == 3) {
            Query query = q_CreateSql("from UserTop where type=? order by gold desc", new Object[]{type});
            query.setMaxResults(10);
            return query.list();
        }
        return new ArrayList<>();
    }

    @Cacheable(value = {"springCache"}, key = "'topList'+#type+'1'")
    public List<User> topList2(int type) {
        if (type == 1) {
            Query query = q_CreateSql("from User where status= 0 order by gameGold desc", new Object[0]);
            query.setMaxResults(10);
            return query.list();
        } else if (type == 2) {
            Query query = q_CreateSql("from User where status= 0 order by level desc", new Object[0]);
            query.setMaxResults(10);
            return query.list();
        }
        return new ArrayList<>();
    }

    @Caching(evict = {@org.springframework.cache.annotation.CacheEvict(value = {"springCache"}, key = "'topList'+#type+'1'"), @org.springframework.cache.annotation.CacheEvict(value = {"springCache"}, key = "'topList'+#type+'2'"), @org.springframework.cache.annotation.CacheEvict(value = {"springCache"}, key = "'topList'+#type+'3'")})
    public void voidGetByType(int type) {
    }

}
